package com.lly.songhigh.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lly.songhigh.R;
import com.lly.songhigh.activity.SearchAlbumDetialsActivity;
import com.lly.songhigh.adapter.SearchSpecialListViewAdapter;
import com.lly.songhigh.constant.URL;
import com.lly.songhigh.entity.SearchSpecial;
import com.lly.songhigh.utils.SharedPreferencesUtils;
import com.lly.songhigh.view.SwipeRefreshLoadingLayout;
import com.lly.songhigh.volley.VolleySingleton;

import java.util.List;

/**
 * Created by dllo on 15/10/24.
 */
public class SearchSpecialAlbumFragment extends BaseFragment implements AdapterView.OnItemClickListener, SwipeRefreshLoadingLayout.OnLoadListener, SwipeRefreshLoadingLayout.OnRefreshListener {
    private ListView specialablumListView;
    private String startUrl = URL.SEARCHSPECIAL;
    private String endUrl;
    private String url;
    private int pageCount, page = 1;
    private SearchSpecialListViewAdapter searchSpecialListViewAdapter;
    private VolleySingleton volleySingleton;
    private List<SearchSpecial.DataEntity> datas;
    private SwipeRefreshLoadingLayout refreshLoadingLayout;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.search_specialalbum, null);
        endUrl = SharedPreferencesUtils.getSharePreStr(getActivity(), "SearchActivityEditText", "endUrl");
        url = startUrl + endUrl;
        specialablumListView = (ListView) view.findViewById(R.id.specialablum_listView);
        refreshLoadingLayout = (SwipeRefreshLoadingLayout) view.findViewById(R.id.album_refreshLoadingLayout);
        refreshLoadingLayout.setOnLoadListener(this);
        refreshLoadingLayout.setOnRefreshListener(this);
        specialablumListView.setOnItemClickListener(this);
        refreshLoadingLayout.setTopColor(R.color.transparentColor, R.color.transparentColor, R.color.transparentColor, R.color.transparentColor);
        refreshLoadingLayout.setBottomColor(R.color.themeColor, R.color.whiteColor, R.color.themeColor, R.color.whiteColor);
        volleySingleton = VolleySingleton.getInstance();
        initData();
        return view;
    }

    private void initData() {
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                SearchSpecial searchSpecial = gson.fromJson(response, SearchSpecial.class);
                datas = searchSpecial.getData();
                searchSpecialListViewAdapter = new
                        SearchSpecialListViewAdapter(getActivity(), datas);
                specialablumListView.setAdapter(searchSpecialListViewAdapter);
                pageCount = searchSpecial.getPageCount();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        volleySingleton.addQueue(request);

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        SearchSpecial.DataEntity dataEntity = (SearchSpecial.DataEntity) parent.getItemAtPosition(position);
        int urlId = dataEntity.getAlbumId();
        Intent intent = new Intent(getActivity(), SearchAlbumDetialsActivity.class);
        intent.putExtra("_url", urlId);
        getActivity().startActivity(intent);
    }

    @Override
    public void onLoad() {
        page++;
        if (page>pageCount){
            refreshLoadingLayout.setLoading(false);
        }else {
            url = URL.SEARCHSPECIAL_LOADURL + page + "&q=" + endUrl;
            StringRequest request = new StringRequest(url, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Gson gson = new Gson();
                    SearchSpecial searchSpecial = gson.fromJson(response, SearchSpecial.class);
                    datas = searchSpecial.getData();
                    searchSpecialListViewAdapter.loadData(datas);
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            });
            volleySingleton.addQueue(request);
            refreshLoadingLayout.setLoading(false);
        }
    }

    @Override
    public void onRefresh() {
        refreshLoadingLayout.setRefreshing(false);
    }
}
